var path = require('path');
var express = require('express');
var webpack = require('webpack');
var config = require('./webpack.config');
var webpackDevMiddleware = require('webpack-dev-middleware');
var webpackHotMiddleware = require('webpack-hot-middleware');
var proxy = require('http-proxy-middleware');

var port = 3000;
var app = express();
var compiler = webpack(config);

// configure proxy middleware context
var context = '/api';                     // requests with this path will be proxied
// configure proxy middleware options
var options = {
    target: 'http://etxdev.chinamons.com', // target host
    changeOrigin: true,               // needed for virtual hosted sites
    ws: true,                         // proxy websockets
    // pathRewrite: {
    //     '^/api': ''
    // },
    // router: {
    //     'http://etxdev.chinamons.com': 'http://localhost:3000'
    // }

};
var apiProxy = proxy(options);

app.use(express.static(path.join(__dirname, 'src')));

app.use(context,apiProxy);

app.use(webpackDevMiddleware(compiler, {
    noInfo: true,
    publicPath: config.output.publicPath
}));

app.use(webpackHotMiddleware(compiler));

app.get('/', function (req, res) {
    res.sendFile(path.join(__dirname, 'index.html'));
});

app.listen(port, function onAppListening(err) {
    if (err) {
        console.error(err);
    } else {
        console.info('==> Webpack 🚧 development server listening on port %s', port);
    }
});